//
//  QASquareCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "QASquareCell.h"

@implementation QASquareCell

+ (CGFloat)calculateHeightWithModel:(CommonQAModel *)model {

    CGFloat askHeight = 40 ;
    CGFloat userinfoHeight;
    NSString *content;
    
    
    if (model.userAnswerList.count > 0) {
        content = model.userAnswerList[0].answerContent;
        userinfoHeight = 40;
    } else {
        content = model.content;
        userinfoHeight = CGFLOAT_MIN;
    }
    
    CGFloat aswHeight = [Utilities getStringHeightWithText:content font:[UIFont systemFontOfSize:14] viewWidth:(TTScreenWidth - 54)] ;

    if (aswHeight < 40) {
        aswHeight = 40;
    }
    
    if (aswHeight > 80) {
        aswHeight = 80;
    }
    
    return askHeight + aswHeight + userinfoHeight;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.userIcon pp_clipCorners:UIRectCornerAllCorners radius:self.userIcon.frame.size.height/2];
    });
}

- (void)fillWithModel:(CommonQAModel *)model {
    
    self.mainTitleLab.text = model.title;

    if (model.userAnswerList.count > 0) {
        
        self.timeLab.text = [NSString stringWithFormat:@"解答于 %@",model.userAnswerList[0].createTime];
        self.subTitleLab.text = model.userAnswerList[0].answerContent;
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:model.userAnswerList[0].imgUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        self.subIcon.hidden = NO;
        self.askInfoControl.hidden = NO;
    } else {
        
        self.subTitleLab.text = model.content;
        
        self.subIcon.hidden = YES;
        self.askInfoControl.hidden = YES;
    }
    
    self.qaCountLab.text = [NSString stringWithFormat:@"%@回答",model.answerNumber];

    //计算高度
    CGFloat userinfoHeight;
    NSString *content;
    if (model.userAnswerList.count > 0) {
        content = model.userAnswerList[0].answerContent;
        userinfoHeight = 40;
    } else {
        content = model.content;
        userinfoHeight = CGFLOAT_MIN;
    }
    
    CGFloat aswHeight = [Utilities getStringHeightWithText:content font:[UIFont systemFontOfSize:14] viewWidth:(TTScreenWidth - 54)] ;

    if (aswHeight < 40) {
        aswHeight = 40;
    }
    
    if (aswHeight > 80) {
        aswHeight = 80;
    }

    self.contentHeight.constant = aswHeight;
    self.askInfoHeight.constant = userinfoHeight;
}

@end
